package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsCategory;
import com.qf.service.Adminservice;
import com.qf.service.BrandService;
import com.qf.service.DtsCategoryService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author jiangjingbo
 * @version V1.0
 * @Project java2203
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/6/21 11:12
 */

/**
 * 商品制造商
 */
@CrossOrigin("*")
@RequestMapping("/brand")
@RestController
public class BrandContorller {
    @Autowired
    private BrandService brandService;

    @Autowired
    private Adminservice adminservice;

    @Autowired
    private DtsCategoryService dtsCategoryService;
    @GetMapping("/list")
    public Object list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(defaultValue = "add_time") String sort,
            @RequestParam(defaultValue = "desc") String desc,
            Integer id,String name){
        IPage<DtsBrand> all = brandService.findAll(page, limit, sort, desc, id, name);
        List<DtsBrand> records = all.getRecords();
        long total = all.getTotal();
        HashMap<String, Object> data = new HashMap<>();
        data.put("total",total);
        data.put("items",records);
        return ResponseUtil.ok(data);
    }

    @RequestMapping("/catAndAdmin")
    public Object catAndAdmin(){
        //查询出管理员名字
        List<CatVo> idAdnUsername = adminservice.findIdAdnUsername();
        HashMap<String, Object> data = new HashMap<>();
//        查询出L1类目
        List<CatVo> catVos = dtsCategoryService.finCatvo();
        System.out.println("====catVos========"+catVos);
        List<DtsCategory> list = dtsCategoryService.findleveL2();
        System.out.println(list);
        List<DtsCategory> dtsCategory = dtsCategoryService.findleveL2();
        //调用方法
        List<CatVo> comparison = comparison(catVos, dtsCategory);


        data.put("adminList",idAdnUsername);
        data.put("categoryList",comparison);
        return ResponseUtil.ok(data);
    }
//    添加
    @PostMapping("/create")  //create
    public Object add(@RequestBody DtsBrand dtsBrand){
        int add = brandService.add(dtsBrand);
        return ResponseUtil.ok();
    }

    @PostMapping("/delete")
    public Object delete(@RequestBody DtsBrand dtsBrand){
        brandService.delete(dtsBrand);
        return ResponseUtil.ok();
    }

    @PostMapping("/update")
    public Object update(@RequestBody DtsBrand dtsBrand){
        brandService.update(dtsBrand);
        return ResponseUtil.ok();

    }
    //二级目录和一级目录对比
    public List<CatVo> comparison( List<CatVo> catVos, List<DtsCategory> dtsCategory) {
        if (catVos == null || dtsCategory == null) {
            throw new RuntimeException("参数为空");
        }
        for (int i = 0; i < catVos.size(); i++) {
//          每次循环完成创建一个新的list集合 用于接受二级类目
            List<CatVo> catVos1 = new ArrayList<>();
            for (int j = 0; j < dtsCategory.size(); j++) {
                if (catVos.get(i).getValue().equals(dtsCategory.get(j).getPid())) {
                    CatVo catVo = new CatVo();
                    catVo.setValue(dtsCategory.get(j).getId());
                    catVo.setLabel(dtsCategory.get(j).getName());
                    catVos1.add(catVo);

                    catVos.get(i).setChildren(catVos1);
                }
            }
        }
        return catVos;
    }
}



